package com.llb666.java.leetcode;

import java.util.Arrays;

/**
 * @author 斌~
 * @version 1.0
 * @date 2021/4/25 下午10:34
 */
public class 寻找数组中心下标 {
    public static void main(String[] args) {
        int [] array = {1,3,7,4,5,6};

        System.out.println(pivotIndex(array));
    }

    private static int pivotIndex(int[] nums) {
        //求数组之和
        int sum = Arrays.stream(nums).sum();
        int total = 0;

        for (int i = 0; i < nums.length; i++) {
            total += nums[i];
            if(total==sum){
                return i;
            }
            sum -=nums[i];
        }

        return -1;
    }
}
